using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using JNPF.Basics.Models;
using JNPF.Basics.Logic;
using JNPF.Utils;
using System.Threading.Tasks;
using System.Linq;
using JNPF.Utils.Controller;
using JNPF.Basics.Logic.Common.Login;
using JNPF.Utils.Extensions;

namespace JNPF.Basics.Controller
{
    [Route("api/Jiushi/[Controller]")]
    public class PointswithdrawalController : BaseApiController
    {
        private PointswithdrawalBll jiushiPointswithdrawalService = new PointswithdrawalBll();
        private BillRuleBll billRuleService = new BillRuleBll();
        private DataInterfaceBll dataInterfaceBll = new DataInterfaceBll();
        private UserBll userBll = new UserBll();
        private OrganizeBll organizeBll = new OrganizeBll();
        private UserextendBll userextendBll = new UserextendBll();

        #region GET

        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<ResponseResult> GetInfo(int id)
        {
            var data = await jiushiPointswithdrawalService.GetInfo(id);
            var output = data.MapTo<PointswithdrawalUpOutput>();
            return Success(output);
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResponseResult> GetList(PointswithdrawalListQueryInput input)
        {
            var data = await jiushiPointswithdrawalService.GetPageList(input);
            var outputList = data.list.MapToList<PointswithdrawalListOutput>();
            outputList.ForEach(o =>
            {
            });
            var output = new PageResult()
            {
                list = outputList,
                pagination = data.pagination
            };
            return Success(output);
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpGet("GetListAdmin")]
        public async Task<ResponseResult> GetListAdmin(PointswithdrawalListQueryInput input)
        {
            input.IsAdmin = true;

            var data = await jiushiPointswithdrawalService.GetPageList(input);
            var outputList = data.list.MapToList<PointswithdrawalListOutput>();
            outputList.ForEach(o =>
            {
            });
            var output = new PageResult()
            {
                list = outputList,
                pagination = data.pagination
            };
            return Success(output);
        }


        #endregion

        #region POST

        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="input">新建输入</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResponseResult> Create([FromBody] PointswithdrawalCrInput input)
        {
            var entity = input.MapTo<PointswithdrawalEntity>();

            UserextendEntity userextendEntity = await userextendBll.GetInfoByUserId(UserProvider.Instance.Get().UserId);

            if (userextendEntity == null)
            {
                return Error("未查询到用户信息");
            }

            if (userextendEntity.PointsPendingWithdrawal < input.points)
            {
                return Error("积分不够");
            }
            entity.PayType = userextendEntity.PayType;
            entity.PayAccount = userextendEntity.PayAccount;
            entity.PayName = userextendEntity.PayName;
            entity.PayBankName = userextendEntity.PayBankName;
            entity.PayBankNumber = userextendEntity.PayBankNumber;


            await jiushiPointswithdrawalService.Create(entity, userextendEntity);
            return Success("提现成功");
        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="id">主键id</param>
        /// <param name="input">编辑输入</param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<ResponseResult> Update(int id, [FromBody] PointswithdrawalCrInputSh input)
        {
            await jiushiPointswithdrawalService.Examine(id, input);
            return Success("审核成功");
        }

        /// <summary>
        /// 删除接口
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ResponseResult> Delete(int id)
        {
            var entity = await jiushiPointswithdrawalService.GetInfo(id);
            var isOk = await jiushiPointswithdrawalService.Delete(entity);
            return isOk > 0 ? Success("删除成功") : Error("删除失败");
        }

        #endregion

    }
}